<template>
    
        <importExcel :map="map" :xlsxMap="xlsxMap"  ref="importStu"
        @submit="submitExcel"
        ></importExcel>
   
</template>
<script>
import importExcel from './importExcel'
import StudentXlsxMap from './studentXlsxMap'
import {dictionaryvalue, getPeroidsList, getSchoolStage, getGradeList} from '@/api/schoolApi'
import {importStudent} from '@/api/studentListApi'
export default {
    components: {
        importExcel
    },
    props: {
        schoolId: String
    },
   
    data(){
        return {
            map: {
                学生姓名: {col: 'name', reg: '', required: true},
                学籍号: {col: 'studentCode', reg: '', noRepeat: true},
                身份证号码: {col: 'idNumber', reg: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, desc: '身份证号码不正确', noRepeat: true},
                性别: {col: 'gender', reg: ''},
                所属学段: {col: 'stageName', required: true, reg: '', list: [], isListNum: false},
                学级: {col: 'startYear', reg: '', required: true, list: [], isListNum: true},
                年级: {col: 'gradeName', required: true, list: [], isListNum: false, isClass: true},
                班级: {col: 'className', reg: /^[0-9一二三四五六七八九]+班$/, desc:'格式如：1班', required: true, isClass: true},
                入学年月: {col: 'startDate', reg: /^[0-9]{4}[\/\.-]{1}[0-9]{1,2}$/, desc: '格式如：2020-01', required: true},

                
                家长姓名: {col: 'parentName', reg: '', required: true},
                // 家长手机号码: {col: 'parentPhone',  reg: /^1[3456789]\d{9}$/, required: true, desc: '手机号码不正确'},
                家长手机号码: {col: 'parentPhone',  reg: '', required: true, desc: '手机号码不正确'},
                // 关系: {col: 'relationCode', reg: '', list: [], isListNum: false},
                关系: {col: 'relationCode', reg: '', required: true},
            },
            xlsxMap: new StudentXlsxMap()
        }
    },
    methods: {
        async importStudentData({list, schoolId}){
            
            for (var item of list) {
                if (item.gender == '男') {
                    item.gender = true
                } else {
                    item.gender = false
                }
            
                item.startDate = this.xlsxMap.formateDate(item.startDate)

                if (item.parentName) {
                    item.parentName = this.xlsxMap.splitByAll(item.parentName)
                }
                if (item.parentPhone) {
                    item.parentPhone = this.xlsxMap.splitByAll(item.parentPhone)
                }
                if (item.relationCode) {
                    item.relationCode = this.xlsxMap.splitByAll(item.relationCode)
                }
                item.parents = []
                for(var i = 0; i<item.parentName.length; i++){
                    var name = item.parentName[i]
                    var phone = item.parentPhone[i]
                    var code = item.relationCode[i]
                    if(!name){
                        name = item.parentName[0]
                    }
                    if(!phone){
                        phone = item.parentPhone[0]
                    }
                    if(!code){
                        code = item.relationCode[0]
                    }
                    item.parents.push({
                        parentName:name,
                        parentPhone: phone,
                        relationCode: code
                    })
                }

                item.startYear = this.xlsxMap.getNumber(item.startYear)
            
            }
            
            var params = {
                    schoolId: schoolId,
                    body: list
                }
            console.log(params)
            await importStudent(params)
        },
        async submitExcel(data){
            console.log('submitExcel:', data)
            try {
                while (true) {
                    var temp = data.splice(0, 500)
                    if (temp.length > 0) {
                        await this.importStudentData({list: temp, schoolId: this.schoolId})
                        this.$refs.importStu.progress ++
                    } else {
                        break
                    }
                }
                        
                this.$message.success('导入学生数据完成')
                this.$refs.importStu.complete()

                this.$store.dispatch('initSchool/putSchool', {
                     schoolId: this.schoolId,
                     initState: 3
                 })

            } catch (error) {
                console.log(error)

                var res = error.data
               
                var details = JSON.parse(res.error.details)
                
                this.$refs.importStu.handleError(details)
            }
        }
    },
    async mounted(){
        let res = await getPeroidsList({
            schoolId: this.schoolId,
            Sorting: 'Name ASC'
        })
        this.xlsxMap.peroidsList = res
        

        res = await getGradeList({
            schoolId: this.schoolId,
            Sorting: 'orderNum'
        })
        var items = res.items
        var list = items.map(obj => obj.name)
        this.xlsxMap.gradeList = list

        // res = await dictionaryvalue({typeCode: 'FamilyRelation'})
        // var list = res.map(obj => obj.valName)
        // this.map['关系'].list = list


        res = await getSchoolStage(this.schoolId)
        var items = res.items
        var list = items.map(obj => obj.name)
        this.map['所属学段'].list = list
    }
}
</script>
<style scoped>

</style>